/*  (c) 2008 Benjamin Schroeter
 *  This file is part of JaDAL - Just another DSL-Tools Addon Library
 *  and licensed under the New BSD License
 *  For more information please visit http://www.codeplex.com/JaDAL */
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling;

namespace BenjaminSchroeter.Dsl.DynamicProperties
{
    public partial class EditDynamicPropertiesDialog : Form
    {
        private readonly DynamicPropertyCollection prop;
        private readonly Store store;

        public EditDynamicPropertiesDialog()
        {
            InitializeComponent();
        }

        public EditDynamicPropertiesDialog(DynamicPropertyCollection prop, Store store) : this()
        {
            this.prop = prop;
            this.store = store;

            FillListBox();
        }

        private void FillListBox()
        {
            this.listBox1.Items.Clear();
            foreach (DynamicProperty property in prop)
                this.listBox1.Items.Add(property);
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if ( this.textBoxName.Text == "" )
                return;

            Type type = Type.GetType(this.comboBox1.Text);
            if (type == null)
            {
                MessageBox.Show(string.Format("Cannot find type {0}.", this.comboBox1.Text),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }

            if( prop.ExistsProperty(this.textBoxName.Text))
            {
                MessageBox.Show(string.Format("The property '{0}' already exists.", this.textBoxName.Text), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;                
            }

            Transaction t = store.TransactionManager.BeginTransaction("Add property to dynamic property collection.");
            prop.AddProperty(this.textBoxName.Text, type, null);
            t.Commit();

            FillListBox();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if ( this.listBox1.SelectedItem == null )
                return;

            DynamicProperty p = (DynamicProperty) this.listBox1.SelectedItem;

            Transaction t = store.TransactionManager.BeginTransaction("Remove property from dynamic property collection");
            prop.RemoveProperty(p.Name);
            t.Commit();

            FillListBox();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null)
                return;

            DynamicProperty p = (DynamicProperty)this.listBox1.SelectedItem;
            if ( p.Type == typeof (DynamicPropertyCollection))
            {
                if (p.Value == null)
                    p.Value = new DynamicPropertyCollection();
                EditDynamicPropertiesDialog f = new EditDynamicPropertiesDialog((DynamicPropertyCollection) p.Value, store);
                f.ShowDialog(this);
            }
        }
    }
}
